<?php
// +----------------------------------------------------------------------
// | 功能说明"
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2030 http://www.jmzh1688.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: lxl
// | jmzhfsauto
// +----------------------------------------------------------------------
namespace app\admin\model;
use think\Db;
use think\Model;

class TrackyaxisModel extends Model
{
    protected $name = 'track_yaxis';
    protected $autoWriteTimestamp = true;//时间自动更新 create_time ,update_time

   protected $field = true;
    public $title = '名称';

    /**
     * 获取y轴数据
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_data($where,$field='*',$orders=['id'=>'asc']){
        $where['isdelete'] = 0;
        $res = $this->where($where)->field($field)->order($orders)->select();
        foreach ($res as $k=>$v){
            $res[$k] = $v->toarray();
        }
        return $res;
    }

    /**
     *
     */
    public function get_level($id){
        return $this->field('level')->find($id)['level'];
    }

    public function edit($param){
        try{
            $result =  $this->update($param);
            if(false === $result){
                return msg(-1, '', $this->getError());
            }else{
                return msg(1,url('Trackxaxis/index'), '修改纵轴数据成功');
            }
        }catch(PDOException $e){
            return msg(-2, '', $e->getMessage());
        }
    }

    public function add($param){
        try{
            $param['isdelete'] = 0;
            if($param['parentid'] == 0){
                $param['level'] = 1;
            }else{
                $level = $this->where('id',$param['parentid'])->value('level');
                $param['level'] = $level+1;
            }
            $result =  $this->save($param);
            if(false === $result){
                return msg(-1, '', $this->getError());
            }else{
                return msg(1,url('Trackxaxis/index'), '添加纵轴项成功');
            }
        }catch(PDOException $e){
            return msg(-2, '', $e->getMessage());
        }
    }
    public function getById($id){
        return $this->where('id', $id)->find();
    }

    public function delById($id){
        try{
            $this->where('id', $id)->update(['isdelete'=>'1']);
            return msg(1, '', '删除成功');
        }catch( PDOException $e){
            return msg(-1, '', $e->getMessage());
        }
    }

    public function getActionWhere($where){
        return $this->where($where)->find();
    }


    public function getListbyRoleId($roleid){
        $para['isdelete']=0;
        $role=Db::name('sys_role')->where('id',$roleid)->find();

        if($roleid==0||$roleid==1){
            $result =  $this->where($para)->order('ordernum asc')->select();
            return $result;
        }

        if(!empty($role)){
            $menut=  config('database.prefix')."sys_role";

            $idstr=" FIND_IN_SET(id , (select menulist from ".$menut." where id=".$roleid."))";
            $result =  $this->where($para)->where($idstr)->order('ordernum asc')->select();


            return $result;

        }
        return null;
    }


    public function getFilsList(){
        return $this->hasMany('AttachmentRelevancy','main_id','id');
    }

    public function getProjeclist($id){
        //$result = $this->with(['getFilsList','getFilsList.getFiles'])->find($id);
        //echo $this->getLastSql();die;
        $a = [
            ['jmzhfs_file_record b','a.file_record_id=b.id','LEFT']
        ];
        $files = Db::name('attachment_relevancy')
            ->alias('a')
            ->join($a)
            ->where("a.isdelete=0 and b.isdelete=0 and a.main_id=$id")
            ->field('a.main_id,b.id as file_id,b.name,b.network_path,b.md5_value,b.size')
            ->select();


        return $files;
    }

    /**
     * 保存上传文件信息
     * @param $param
     * @return array
     * @author: pan
     * @Date: times
     */
    public function saveDetails($param){

        $id = $param['detailsid'];
        $fileids = $param['multifiles'];
        $type = $param['type'];

        if($id){

            if($fileids){
                $fileidarr = explode(',',$fileids);
                $f = [];
                foreach ($fileidarr as $v){
                    $where = "main_id=$id and file_record_id=$v and isdelete=0";
                    $attachment = Db::name('attachment_relevancy')->where($where)->find();
                    if($attachment){
                        continue;
                    }else{
                        $f[] = [
                            'main_id'=>$id,
                            'file_record_id'=>$v,
                            'isdelete'=>0,
                            'table_name'=>$this->name,
                            'create_time'=>date('Y-m-d H:i:s',time())
                        ];
                    }

                }
                if($f){
                    $result = Db::name('attachment_relevancy')->insertAll($f);
                    if(!$result){
                        $ret = array('code'=>-1,'msg'=>'保存失败');
                    }else{
                        $ret = array('code'=>1,'msg'=>'保存成功');
                    }
                }else{
                    $ret = array('code'=>1,'msg'=>'保存成功');
                }

            }else{
                $ret = array('code'=>1,'msg'=>'保存成功');
            }

        }else{
            $ret = array('code'=>-1,'msg'=>'保存失败');
        }

        return $ret;
    }

}
